<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div slot="header" class="clearfix">
        <span>学校筛选</span>
        <el-button style="float: right; padding: 3px 0" type="text" @click="handleSearchList()">
          <i class="el-icon-search"></i>搜索
        </el-button>
      </div>
      <div class="filter-item">
        <el-input
          v-model="listQuery.keyword"
          placeholder="学校名称"
          style="width: 200px;"
          prefix-icon="el-icon-search"
          clearable
        />
        <el-button @click="handleSearchList()" style="margin-left: 10px;">搜索</el-button>
        <el-button @click="listQuery = Object.assign({}, defaultListQuery); handleSearchList()">重置</el-button>
      </div>
      <div class="action-buttons">
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="handleAddSchool"
        >
          添加学校
        </el-button>
      </div>
    </el-card>

    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
    </el-card>

    <div class="table-container">
      <el-table
        ref="schoolTable"
        :data="list"
        style="width: 100%;"
        v-loading="listLoading"
        border
      >
        <el-table-column label="编号" width="100" align="center">
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="学校名称" min-width="200">
          <template slot-scope="scope">
            <span>{{ scope.row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="学校代码" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.code }}</template>
        </el-table-column>
        <el-table-column label="所在地" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.location }}</template>
        </el-table-column>
        <el-table-column label="创办时间" width="160" align="center">
          <template slot-scope="scope">{{ scope.row.establishmentDate }}</template>
        </el-table-column>
        <el-table-column label="学校类型" width="120" align="center">
          <template slot-scope="scope">{{ scope.row.type }}</template>
        </el-table-column>
        <el-table-column label="创建时间" width="160" align="center">
          <template slot-scope="scope">{{ scope.row.createTime }}</template>
        </el-table-column>
        <el-table-column label="状态" width="100" align="center">
          <template slot-scope="scope">
            <el-tag :type="scope.row.status | statusFilter">
              {{ scope.row.status | statusTextFilter }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleUpdateSchool(scope.$index, scope.row)"
            >
              编辑
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleViewDepartments(scope.$index, scope.row)"
            >
              院系
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleDeleteSchool(scope.$index, scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes, prev, pager, next, jumper"
        :current-page.sync="listQuery.page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="listQuery.size"
        :total="total"
      />
    </div>

    <!-- 添加学校对话框 -->
    <el-dialog
      title="添加学校"
      :visible.sync="addDialogVisible"
      width="500px"
    >
      <el-form
        ref="addForm"
        :model="addForm"
        :rules="addRules"
        label-width="100px"
      >
        <el-form-item label="学校名称" prop="name">
          <el-input v-model="addForm.name" placeholder="请输入学校名称" />
        </el-form-item>
        <el-form-item label="学校代码" prop="code">
          <el-input v-model="addForm.code" placeholder="请输入学校代码" />
        </el-form-item>
        <el-form-item label="所在地" prop="location">
          <el-input v-model="addForm.location" placeholder="请输入所在地" />
        </el-form-item>
        <el-form-item label="创办时间" prop="establishmentDate">
          <el-date-picker
            v-model="addForm.establishmentDate"
            type="date"
            placeholder="请选择创办时间"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item label="学校类型" prop="type">
          <el-input v-model="addForm.type" placeholder="请输入学校类型" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="addForm.status" placeholder="请选择状态">
            <el-option label="禁用" :value="0" />
            <el-option label="启用" :value="1" />
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitAddForm">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 编辑学校对话框 -->
    <el-dialog
      title="编辑学校"
      :visible.sync="editDialogVisible"
      width="500px"
    >
      <el-form
        ref="editForm"
        :model="editForm"
        :rules="editRules"
        label-width="100px"
      >
        <el-form-item label="学校名称" prop="name">
          <el-input v-model="editForm.name" placeholder="请输入学校名称" />
        </el-form-item>
        <el-form-item label="学校代码" prop="code">
          <el-input v-model="editForm.code" placeholder="请输入学校代码" />
        </el-form-item>
        <el-form-item label="所在地" prop="location">
          <el-input v-model="editForm.location" placeholder="请输入所在地" />
        </el-form-item>
        <el-form-item label="创办时间" prop="establishmentDate">
          <el-date-picker
            v-model="editForm.establishmentDate"
            type="date"
            placeholder="请选择创办时间"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item label="学校类型" prop="type">
          <el-input v-model="editForm.type" placeholder="请输入学校类型" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="editForm.status" placeholder="请选择状态">
            <el-option label="禁用" :value="0" />
            <el-option label="启用" :value="1" />
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEditForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getSchoolPage, deleteSchool, addSchool, getSchoolById, updateSchool } from '@/api/edu'

const defaultListQuery = {
  page: 1,
  size: 10,
  keyword: null
}

export default {
  name: 'SchoolList',
  filters: {
    statusFilter(status) {
      const statusMap = {
        0: 'info',
        1: 'success'
      }
      return statusMap[status]
    },
    statusTextFilter(status) {
      const statusTextMap = {
        0: '禁用',
        1: '启用'
      }
      return statusTextMap[status]
    }
  },
  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      total: 0,
      listLoading: false,
      addDialogVisible: false,
      editDialogVisible: false,
      addForm: {
        name: '',
        code: '',
        location: '',
        establishmentDate: '',
        type: '',
        status: 1
      },
      editForm: {
        id: null,
        name: '',
        code: '',
        location: '',
        establishmentDate: '',
        type: '',
        status: 1
      },
      addRules: {
        name: [
          { required: true, message: '请输入学校名称', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入学校代码', trigger: 'blur' }
        ],
        location: [
          { required: true, message: '请输入所在地', trigger: 'blur' }
        ],
        establishmentDate: [
          { required: true, message: '请选择创办时间', trigger: 'change' }
        ],
        type: [
          { required: true, message: '请输入学校类型', trigger: 'blur' }
        ]
      },
      editRules: {
        name: [
          { required: true, message: '请输入学校名称', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入学校代码', trigger: 'blur' }
        ],
        location: [
          { required: true, message: '请输入所在地', trigger: 'blur' }
        ],
        establishmentDate: [
          { required: true, message: '请选择创办时间', trigger: 'change' }
        ],
        type: [
          { required: true, message: '请输入学校类型', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleSearchList() {
      this.listQuery.page = 1
      this.getList()
    },
    handleAddSchool() {
      // 重置表单
      this.addForm = {
        name: '',
        code: '',
        location: '',
        establishmentDate: '',
        type: '',
        status: 1
      }
      this.addDialogVisible = true
    },
    handleUpdateSchool(index, row) {
      // 获取学校详情
      getSchoolById(row.id).then(response => {
        this.editForm = { ...response.data }
        this.editDialogVisible = true
      })
    },
    handleViewDepartments(index, row) {
      this.$router.push(`/edu/school/${row.id}/departments`)
    },
    handleDeleteSchool(index, row) {
      this.$confirm('是否要删除该学校?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteSchool(row.id).then(response => {
          this.$message({
            message: '删除成功',
            type: 'success',
            duration: 1000
          })
          this.getList()
        })
      })
    },
    handleSizeChange(val) {
      this.listQuery.size = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    getList() {
      this.listLoading = true
      getSchoolPage(this.listQuery).then(response => {
        this.listLoading = false
        this.list = response.data.records
        this.total = response.data.total
      }).catch(() => {
        this.listLoading = false
      })
    },
    submitAddForm() {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          addSchool(this.addForm).then(response => {
            this.$message({
              message: '添加成功',
              type: 'success',
              duration: 1000
            })
            this.addDialogVisible = false
            this.getList()
          })
        }
      })
    },
    submitEditForm() {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          updateSchool(this.editForm).then(response => {
            this.$message({
              message: '更新成功',
              type: 'success',
              duration: 1000
            })
            this.editDialogVisible = false
            this.getList()
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.filter-container {
  margin-bottom: 15px;
}

.operate-container {
  margin-bottom: 15px;
}

.table-container {
  margin-bottom: 15px;
}

.pagination-container {
  display: inline-block;
  float: right;
}

.action-buttons {
  display: flex;
  justify-content: flex-end;
  margin-top: -10px;
  margin-bottom: 10px;
}
</style>